<template>
	<form-container title="添加" :width="550" :visible="visible" :destroy-on-close="true" @close="onClose">
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="测试名称：" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入主题" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="sendLoading">发送</a-button>
		</template>
	</form-container>
	<user-selector-plus
		ref="UserSelectorPlus"
		:org-tree-api="selectorApiFunction.orgTreeApi"
		:user-page-api="selectorApiFunction.userPageApi"
		:checked-user-list-api="selectorApiFunction.checkedUserListApi"
		@onBack="userBack"
	/>
</template>

<script setup name="messageForm">
	import { required } from '@/utils/formRules'
	import { message } from 'ant-design-vue'
	import testApi from '@/api/product/productApi'
	import userSelectorPlus from '@/components/Selector/userSelectorPlus.vue'
	import tool from '@/utils/tool'
	import userCenterApi from '@/api/sys/userCenterApi'
	import userApi from '@/api/sys/userApi'

	const sendLoading = ref(false)
	let UserSelectorPlus = ref()
	// 定义emit事件
	const emit = defineEmits({ successful: null })
	// 默认是关闭状态
	let visible = ref(false)
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	let userList = ref([])
	// 打开抽屉
	const onOpen = () => {
		visible.value = true
		formData.value = {}
	}
	// 关闭抽屉
	const onClose = () => {
		emit('successful')
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
		name: [required('请输入名称')], 
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {

			testApi
				.AddProduct(formData.value)
				.then(() => {
					message.success('发送成功')
					onClose()
				})
				.finally(() => {
					sendLoading.value = false
				})
		})
	}

	// 传递设计器需要的API
	const selectorApiFunction = {
		orgTreeApi: (param) => {
			return testApi.Add(param).then((data) => {
				return Promise.resolve(data)
			})
		}
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
